package ch.il07.converter.presentation.beans;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import ch.il07.converter.business.ServiceFactoryAdapter;
import ch.il07.converter.business.UnitAction;
import ch.il07.converter.persistence.entities.Unit;
import ch.il07.converter.presentation.Helper;
import ch.il07.converter.presentation.beans.entities.UnitBean;

public class ControllerUnitBean {
	private List<UnitBean> units = null;
	private String owner = null;
	private String measurandName = null;
	
	public ControllerUnitBean() {
		Map<String, String> params = Helper.getExternalContext().getRequestParameterMap();
		HttpSession s = Helper.getSession();

		owner = (String) params.get("owner");
		measurandName = (String) params.get("name");
		
		if(owner == null) {
			owner = (String) s.getAttribute("owner");
		} else {
			s.setAttribute("name", measurandName);
		}
		
		if(measurandName == null) {
			measurandName = (String) s.getAttribute("name");
		} else {
			s.setAttribute("owner", owner);
		}
		
		loadAllUnits();
	}
	
	private void loadAllUnits() {
		units = new ArrayList<UnitBean>();
		UnitAction ua = ServiceFactoryAdapter.getUnitActionInstance();
		List<Unit> privateUnits = ua.getPrivateUnitsMeasurand(measurandName, owner);
		
//		XXX Currently administrative operations are not implemented
//		if(Helper.isAdministrator()) {
//			if(privateUnits == null) {
//				privateUnits = new ArrayList<Unit>();
//			}
//			for(Unit u : ua.getPublicUnitsMeasurand(measurandName)) {
//				privateUnits.add(u);
//			}
//		}
		
		displayUnits(privateUnits);
	}
	
	private void displayUnits(List<Unit> privateUnits) {
		for(Unit u : privateUnits) {
			String owner = u.getOwner() != null ? u.getOwner().getUsername() : null;
			UnitBean ub = new UnitBean(
					u.getName(),
					owner,
					u.getShift(),
					u.getSymbol(),
					u.getDivisor()
			);
			ub.setUnitId("" + u.getUnitId());
			units.add(ub);
		}
	}
	
	private List<UnitBean> getSelectedUnits() {
		List<UnitBean> selected = new ArrayList<UnitBean>();
		for(UnitBean ub : units) {
			if(ub.isSelected()) {
				selected.add(ub);
			}
		}
		return selected;
	}

	public String vote() {
		// XXX not implemented yet
		return "stay";
	}
	
	public String delete() {
		UnitAction ua = ServiceFactoryAdapter.getUnitActionInstance();
		for(UnitBean ub : getSelectedUnits()) {
			ua.deleteUnit(Helper.getUserName(), Integer.parseInt(ub.getUnitId()));
		}
		loadAllUnits();
		return "stay";
	}

	public String save() {
		UnitAction ua = ServiceFactoryAdapter.getUnitActionInstance();
		for(UnitBean ub : getSelectedUnits()) {
			ua.editUnit(Helper.getUserName(), Integer.parseInt(ub.getUnitId()), ub.getName(), ub.getDivisor(), ub.getShift(), ub.getSymbol());
		}
		loadAllUnits();
		return "stay";
	}

	/**
	 * @return the units
	 */
	public List<UnitBean> getUnits() {
		return units;
	}

	/**
	 * @param units the units to set
	 */
	public void setUnits(List<UnitBean> units) {
		this.units = units;
	}
}